__author__ = 'Forcs'

from django.http import HttpResponse

from result_code import ResultCode

import json

def send_response_with_result(result):
    return HttpResponse(result, content_type="application/json;charset=utf-8")

def send_error_response_on_exception(err):
    return send_error_response_with_message(message=str(err) if err else u'')

def send_ok_response_with_data(data, rc=ResultCode.OK, message=u''):
    res = {
        "resultCode": rc,
        "message": message.decode('utf8') if message else '',
        "data": data
    }
    return send_response_with_result(json.dumps(res))

def send_ok_response_with_list(list, rc=ResultCode.OK, message=u''):
    return send_ok_response_with_data(data={
        "list": list
    }, rc=rc, message=message)


def send_error_response_with_message(message=u'', rc=ResultCode.ERROR, data=None):
    res = {
        "resultCode": rc,
        "message": message.decode('utf8') if message else '',
    }
    if data:
        res["data"] = data
    return send_response_with_result(json.dumps(res))
